﻿/*
#**************************************************************************
#  openSIS is a student information system for public and non-public 
#  schools from Open Solutions for Education, Inc. web: www.os4ed.com
#
#  openSIS is  web-based, open source, and comes packed with features that 
#  include student demographic info, scheduling, grade book, attendance, 
#  report cards, eligibility, transcripts, parent portal, 
#  student portal and more.   
#
#  Visit the openSIS web site at http://www.opensis.com to learn more.
#  If you have question regarding this system or the license, please send 
#  an email to info@os4ed.com.
#
#  This program is released under the terms of the GNU General Public License  
#  as  published by the Free Software Foundation, version 2 of the License. 
#  See license.txt.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#**************************************************************************** 
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;




/// <summary>
/// Summary description for DynamicTranslation
/// </summary>
/// 
namespace OSISDynamicTranslation
{
    public class DynamicTranslation
    {
        public DynamicTranslation()
        {
            //
            // TODO: Add constructor logic here
            //

        }

        public string Translate(string stringToTranslate, string fromLanguage, string toLanguage)
        {
          
            // make sure that the passed string is not empty or null

            // per Google's terms of use, we can only translate
            // a string of up to 5000 characters long

            if (toLanguage == "zh-CHS")
            {
                toLanguage = "zh";
            
            }

            if (stringToTranslate.Length <= 5000)
            {
                const int bufSizeMax = 65536;
                const int bufSizeMin = 8192;

                try
                {
                    // by default format? is text.  
                    // so we don't need to send a format? key
                    string requestUri = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=" + stringToTranslate + "&langpair=" + fromLanguage + "%7C" + toLanguage;

                    // execute the request and get the response stream
                    System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(requestUri);
                    System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
                    System.IO.Stream responseStream = response.GetResponseStream();

                    // get the length of the content returned by the request
                    int length = (int)response.ContentLength;
                    int bufSize = bufSizeMin;

                    if (length > bufSize)
                        bufSize = length > bufSizeMax ? bufSizeMax : length;

                    // allocate buffer and StringBuilder for reading response
                    byte[] buf = new byte[bufSize];
                    System.Text.StringBuilder sb = new System.Text.StringBuilder(bufSize);

                    // read the whole response
                    while ((length = responseStream.Read(buf, 0, buf.Length)) != 0)
                    {
                        sb.Append(System.Text.Encoding.UTF8.GetString(buf, 0, length));
                    }

                    // the format of the response is like this
                    // {"responseData": {"translatedText":"¿Cómo estás?"}, 
                    //"responseDetails": null, "responseStatus": 200

                    string translatedText = sb.Remove(0, 36).ToString();
                    translatedText = translatedText.Substring(0, translatedText.IndexOf("\"},"));

                    return translatedText;

                }
                // so now let's clean up the response by manipulating the string
                catch
                {
                    return null;
                }

            }
            else
            {

                return null;

            }


        }


       


        



    }
}